package com.zhengke.controller.cggl;

import com.zhengke.common.BaseController;
import com.zhengke.common.object.AjaxDone;
import com.zhengke.common.object.Result;
import com.zhengke.common.util.DateUtil;
import com.zhengke.pojo.MallSupplier;
import com.zhengke.pojo.MallSupplierChannel;
import com.zhengke.service.cggl.CGGL020Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * *******************************
 * 创建时间：2019/12/31 10:39
 * 项目名称：tvmall-parent
 * 文件名称：null.java
 * 类说明：  采购管理控制层
 *
 * @author 王鑫
 * @version 1.0
 * *******************************
 */
@Controller
@RequestMapping("CGGL020")
public class CGGL020Controller extends BaseController {

    @Autowired
    private CGGL020Service service;

    /**
     * 跳转渠道管理列表页
     *
     * @return
     */
    @RequestMapping("query.do")
    public String query(ModelMap model) throws Exception {
        List<MallSupplier> mallSupplier = service.queryMallSupplier();
        model.put("data",mallSupplier);
        return "cggl/cggl020";
    }

    /**
     * 渠道管理列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("mlsList.do")
    public Result list(String channelName, String supplierId, int page, int limit) {

        Result rs = null;
        try {
            rs = service.queryChannel(channelName, supplierId, page, limit);
            return rs;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result();
    }


    /**
     * 跳转添加渠道页
     *
     * @return
     */
    @RequestMapping("add.do")
    public String add(ModelMap model) throws Exception {
        List<MallSupplier> mallSupplier = service.queryMallSupplier();
        model.put("data",mallSupplier);
        return "cggl/cggl0202";
    }

    /**
     * 添加渠道
     * @param request
     * @return
     */
    @RequestMapping("addChannel.do")
    @ResponseBody
    public AjaxDone addChannel(HttpServletRequest request){
        String channelName = request.getParameter("channelName");
        String supplierId = request.getParameter("supplierId");
        String channelCode = request.getParameter("channelCode");
        String channelDesc = request.getParameter("channelDesc");

        try {
            MallSupplierChannel msChannel = new MallSupplierChannel();
            msChannel.setChannelCode(channelCode);
            msChannel.setChannelDesc(channelDesc);
            msChannel.setSupplierId(Integer.parseInt(supplierId));
            msChannel.setChannelName(channelName);
            msChannel.setCreateTime(DateUtil.getSystemDateTime());

            Integer result = service.save(msChannel);
            if(result <= 0){
                return new AjaxDone("300","请求失败");
            }
            return new AjaxDone("200","请求成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxDone("300","请求失败");

    }

    /**
     * 删除渠道
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("delChannel.do")
    public AjaxDone delChannel(HttpServletRequest request) throws Exception {
        String channelId = request.getParameter("channelId");
        int result = service.delete(Integer.parseInt(channelId));
        if(result <= 0){
            return new AjaxDone("300","删除失败");
        }
        return new AjaxDone("200","删除成功");
    }

    /**
     * 跳转到渠道修改页
     * @return
     */
    @RequestMapping("editChannelPage.do")
    public String editChannelPage(String supplierName, ModelMap model) throws Exception {
        /*List<MallSupplier> mallSupplierList = service.queryMsBySupplierId(Integer.parseInt(supplierId));
        model.put("data",mallSupplierList);*/

        List<MallSupplier> mallSupplier = service.queryMallSupplier();
        model.put("data",mallSupplier);
        model.put("supplierName",supplierName);
        return "cggl/cggl0203";
    }

    /**
     * 修改渠道
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("editChannel.do")
    public AjaxDone editChannel(HttpServletRequest request){
        String channelId = request.getParameter("channelId");
        String channelName = request.getParameter("channelName");
        String supplierId = request.getParameter("supplierId");
        String channelCode = request.getParameter("channelCode");
        String channelDesc = request.getParameter("channelDesc");

        try {
            MallSupplierChannel msChannel = (MallSupplierChannel) service.queryMsByChannelId(Integer.parseInt(channelId));
            msChannel.setChannelCode(channelCode);
            msChannel.setChannelDesc(channelDesc);
            msChannel.setSupplierId(Integer.parseInt(supplierId));
            msChannel.setChannelName(channelName);
            msChannel.setCreateTime(DateUtil.getSystemDateTime());

            Integer result = service.update(msChannel);
            if(result <= 0){
                return new AjaxDone("300","修改失败");
            }
            return new AjaxDone("200","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxDone("300","修改失败");

    }

}
